package com.rem.chat.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.rem.chat.aop.annotation.GlobalInterceptor;
import com.rem.chat.common.constants.Constants;
import com.rem.chat.common.errcode.ControllerErrorCodeConstants;
import com.rem.chat.common.exception.ControllerException;
import com.rem.chat.common.utils.RedisComponent;
import com.rem.chat.common.utils.RedisUtil;
import com.rem.chat.pojo.request.UserLoginRequest;
import com.rem.chat.pojo.request.UserRegisterRequest;
import com.rem.chat.pojo.response.CheckCodeResponse;
import com.rem.chat.pojo.response.CommonResult;
import com.rem.chat.service.AccountService;
import com.rem.chat.pojo.response.UserLoginResponse;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @author: Themberfue
 * @date: 2025/6/4 12:28
 * @description:
 */
@RestController("accountController")
@RequestMapping("/account")
@Validated
public class AccountController {
    private static final Logger log = LoggerFactory.getLogger(AccountController.class);

    @Resource
    RedisUtil redisUtil;

    @Resource(name = "accountServiceImpl")
    AccountService accountService;

    @Resource
    private RedisComponent redisComponent;

    @GetMapping("/checkCode")
    public CommonResult<CheckCodeResponse> checkCode() {
        // Step 1. 生成验证码
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(100, 42);

        String code = captcha.getCode();
        log.info("图形验证码为：{}", code);

        // Step 2. 创建响应验证码
        String checkCode = captcha.getImageBase64Data();
        String checkCodeKey = UUID.randomUUID().toString();
        CheckCodeResponse checkCodeResponse = new CheckCodeResponse(checkCode, checkCodeKey);

        // Step 3. 存入缓存中
        String key = Constants.REDIS_KEY_CHECK_CODE + checkCodeKey;
        redisUtil.set(key, code/*, Constants.REDIS_KEY_TIME_1MIN * 10*/); // ! 暂时没有存时间，利好测试

        // Step 4. 返回结果
        return CommonResult.success(checkCodeResponse);
    }

    @PostMapping("/register")
    public CommonResult<Boolean> register(@RequestBody @Validated UserRegisterRequest request) {
        log.info("register UserRegisterRequest：{}", request);

        String key = Constants.REDIS_KEY_CHECK_CODE + request.getCheckCodeKey();
        try {
            // Step 1. 校验验证码是否正确
            if (!request.getCheckCode()
                    .equalsIgnoreCase(redisUtil.get(key))) {
                throw new ControllerException(ControllerErrorCodeConstants.CHECK_CODE_ERROR);
            }
            return CommonResult.success(
                    accountService.register(request)
            );
        } finally {
//            redisUtil.delete(key);
        }
    }

    @PostMapping("/login")
    public CommonResult<UserLoginResponse> login(@RequestBody @Validated UserLoginRequest request) {
        log.info("login UserLoginRequest：{}", request);

        String key = Constants.REDIS_KEY_CHECK_CODE + request.getCheckCodeKey();
        try {
            // Step 1. 校验验证码是否正确
            if (!request.getCheckCode()
                    .equalsIgnoreCase(redisUtil.get(key))) {
                throw new ControllerException(ControllerErrorCodeConstants.CHECK_CODE_ERROR);
            }
            return CommonResult.success(
                    accountService.login(request));
        } finally {
//            redisUtil.delete(key);
        }
    }
}
